package com.ybkj.o2o.zhuanxian.controller.res;

import com.google.common.base.Preconditions;

import com.ybkj.o2o.zhuanxian.common.error.ErrorConstantV4;
import com.ybkj.o2o.zhuanxian.common.error.ResultTransport;
import com.ybkj.o2o.zhuanxian.entity.Payrecordorder;
import com.ybkj.o2o.zhuanxian.manager.PayRecordOrderManager;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/payRecordOrder")
public class PayRecordOrderController {

  @Autowired
  private PayRecordOrderManager payRecordOrdermanager;

  /**
   * 添加充值记录 .
   * 
   * @param orderId     订单id
   * @param money       金额
   * @param payRecordTypeOrder payRecordTypeOrder
   * @param appKey      区分系统
   * 
   */
  @RequestMapping(value = "create", method = RequestMethod.POST)
  @ResponseBody
  public ResultTransport create(
      @RequestParam("orderId") Long orderId,
      @RequestParam("money") Double money,
      @RequestParam("payRecordTypeOrder") String payRecordTypeOrder,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(orderId != null, "订单Id不能为空");

    try {
      Payrecordorder payrecordorder = 
          payRecordOrdermanager.insert(orderId, money, payRecordTypeOrder);

      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(payrecordorder);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;
  }

  /**
   * 查询充值接口 .
   * @param id      id
   * @param appKey 区分系统
   */
  @RequestMapping(value = "findOne", method = RequestMethod.GET)
  @ResponseBody
  public ResultTransport findOne(
      @RequestParam("id") Long id, 
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    try {
      Payrecordorder payRecordOrder = payRecordOrdermanager.findByIdAndAppKey(id);

      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(payRecordOrder);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;
  }

  /**
   * 修改充值记录 .
   * @param id      id
   * @param appKey  区分系统
   */
  @RequestMapping(value = "update", method = RequestMethod.PUT)
  @ResponseBody
  public ResultTransport update(
      @RequestParam("id") Long id, 
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {
      Payrecordorder payRecordOrder = payRecordOrdermanager.update(id);

      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(payRecordOrder);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;
  }

}
